package la.iok.hzsvn.lewin.movie.operation.controller;

import la.iok.hzsvn.lewin.movie.core.Token;
import la.iok.hzsvn.lewin.movie.operation.model.LuckyMoneyVo;
import la.iok.hzsvn.lewin.movie.operation.service.LuckyMoneyService;
import la.iok.hzsvn.lewin.mybatis.model.PageVo;
import la.iok.hzsvn.share.response.OperatorResponse;
import la.iok.hzsvn.share.response.OperatorResponses;
import la.iok.hzsvn.share.response.SingleDataResponse;
import la.iok.hzsvn.share.tools.datetime.DateTimeUtil;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("luckyMoney")
public class LuckyMoneyController {
    private final LuckyMoneyService luckyMoneyService;

    public LuckyMoneyController(LuckyMoneyService luckyMoneyService) {
        this.luckyMoneyService = luckyMoneyService;
    }

    @GetMapping
    public SingleDataResponse<PageVo<LuckyMoneyVo>> page(
            @RequestHeader Token token, String orderColumn, String order, int page, int limit,
            Long orgUnitId, Long roomId, String start, String end){
        page = Math.max(1,page);
        limit = Math.min(Math.max(1,limit),50);
        Date s = DateTimeUtil.safelyParseDate(start,"yyyy-MM");
        Date e = DateTimeUtil.safelyParseDate(end,"yyyy-MM");
        if(s != null){
            s = DateTimeUtil.setTime(DateTimeUtil.monthStart(s),0,0,0,0);
        }
        if(e != null){
            e = DateTimeUtil.setTime(DateTimeUtil.monthEnd(e),23,59,59,999);
        }
        PageVo<LuckyMoneyVo> pageRecords=luckyMoneyService.page(token,page,limit,orderColumn,order,orgUnitId,roomId,s,e);
        return new SingleDataResponse<>(pageRecords);
    }

    @PutMapping("returnMoney/{status}")
    public OperatorResponse onOff(@RequestHeader Token token, @PathVariable String status){
        luckyMoneyService.switchStatus(token,status);
        return OperatorResponses.success();
    }

    @GetMapping("returnMoney")
    public SingleDataResponse<String> returnMoneyStatus(@RequestHeader Token token) {
        String status = luckyMoneyService.switchStatus(token);
        SingleDataResponse<String> res = new SingleDataResponse<>();
        res.setData(status);
        return res;
    }
}
